package com.bjpower.services.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpower.common.enums.PublicStatus;
import com.bjpower.common.enums.SysCode;
import com.bjpower.common.exceptions.type.SysException;
import com.bjpower.domain.UserAccount;
import com.bjpower.domain.UserInfo;
import com.bjpower.mapper.UserAccountMapper;
import com.bjpower.mapper.UserInfoMapper;
import com.bjpower.models.request.MerchINfoRequest;
import com.bjpower.models.request.UserPageInfoRequest;
import com.bjpower.services.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;
    @Override
    public IPage<UserInfo> getUserInfoList(UserPageInfoRequest userPageInfoRequest) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        IPage<UserInfo> page = userInfoMapper.selectPage(
                Page.of(userPageInfoRequest.getPageNum(), userPageInfoRequest.getPageSize()), queryWrapper);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertMerchInfo(MerchINfoRequest merchINfoRequest) {
        //判断用户是进行蟹盖操作还是插入操作通过merchINfoRequest.getUserNo()判断
        if(!(StrUtil.isBlank(merchINfoRequest.getUserNo()))){
            //进行编辑更改操作
            //根据userNo查询出用户信息
            UpdateWrapper<UserInfo> wrapper = new UpdateWrapper<UserInfo>();
            wrapper.eq("user_no",merchINfoRequest.getUserNo())
                    .set("phone",merchINfoRequest.getPhone())
                    .set("login_name",merchINfoRequest.getLoginName())
                    .set("user_email",merchINfoRequest.getUserEmail())
                    .set("shop_name",merchINfoRequest.getShopName())
                    .set("shop_scope",merchINfoRequest.getShopScope());
            int updateCount = userInfoMapper.update(wrapper);
            if (updateCount == 0){
                throw new SysException(SysCode.MERCH_NOT_EXIST);
            }

        }else {
            //进行创建操作
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name",merchINfoRequest.getLoginName());
            Long count = userInfoMapper.selectCount(wrapper);
            //商户名是否存在
            if (count > 0){
                throw new SysException(SysCode.MERCH_NAME_REPEAT);
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setId(IdWorker.get32UUID());
            userInfo.setUserNo("8" + IdWorker.getId());
            userInfo.setLoginName(merchINfoRequest.getLoginName());
            userInfo.setPhone(merchINfoRequest.getPhone());
            userInfo.setAccountNo("9"+ IdWorker.getId());
            userInfo.setPassword(IdWorker.get32UUID());
            userInfo.setPayPwd(IdWorker.get32UUID());
            userInfo.setShopName(merchINfoRequest.getShopName());
            userInfo.setShopScope(merchINfoRequest.getShopScope());
            userInfo.setUserEmail(merchINfoRequest.getUserEmail());
            userInfo.setStatus(PublicStatus.ENABLE.name());
            userInfo.setCreateTime(new Date());
            userInfo.setRemark("创建商户");
            userInfoMapper.insert(userInfo);
            //插入资金账号
            UserAccount userAccount = new UserAccount();
            userAccount.setId(IdWorker.get32UUID());
            userAccount.setUserNo(userInfo.getUserNo());
            userAccount.setAccountNo(userInfo.getAccountNo());
            userAccount.setBalance(new BigDecimal(0));
            userAccount.setTotalIncome(new BigDecimal(0));
            userAccount.setTotalExpend(new BigDecimal(0));
            userAccount.setStatus(PublicStatus.ENABLE.name());
            userAccountMapper.insert(userAccount);
        }
    }

    @Override
    public UserInfo getUserInfoByUserNo(String userNo) {
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_no", userNo));
        return userInfo;
    }

    @Override
    public List<UserInfo> getMerchShopNameList(String shopName) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.like("shop_name",shopName);
        List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
        return userInfoList;
    }
}
